package org.example.controller;

import org.example.domain.Novel;
import org.example.service.NovelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> 测试控制器 </p>
 *
 * <a href="https://blog.csdn.net/weixin_59610127/article/details/123802472">参考</a>
 *
 * @author Chris
 * @since 2022/10/25 10:54
 */

@RestController
@RequestMapping(value = "/", produces = "text/plain;charset=utf-8")
public class NovelController {
    /**
     * 使用 slf4j 日志框架
     */
    private final static Logger log = LoggerFactory.getLogger(NovelController.class);

    @Autowired
    private NovelService novelService;

    @RequestMapping("/novel")
    public String novel(Novel novel) {
        log.info("{}", novel);
        final List<Novel> novels = novelService.listNovel(novel);
        final String collect = novels.stream().map(Novel::getNovelName).collect(Collectors.joining(","));
        log.info("{}", collect);

        return collect;
    }


    @RequestMapping("/saveNovel")
    public String saveNovel(Novel novel) {
        log.info("{}", novel);

        novel.setNovelId(1L);
        novel.setNovelName("测试");
        novel.setNovelAuthor("测试");
        novel.setNovelWords(2L);
        novel.setDetailUrl("https://www.baidu.com/");
        novel.setStatus(1);

        novelService.save(novel);

        return "删除成功";
    }

}